<template>
  <div class="app-container">
    <!-- 添加或修改租约保理申请对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px"  label-position="top">
        <el-row>
          <el-col :span="10">
            <el-form-item label="金融产品选择" prop="jrProductId">
              <el-select v-model="form.jrProductId" placeholder="金融产品选择" @change="selectCredLion">
                <el-option :label="item.jrProductName" :value="item.id" v-for="item in products"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="授信额度" prop="creditLine">
              <el-input v-model="form.creditLine" placeholder="请输入授信额度" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="年一次性服务费" prop="serviceFee">
              <el-input v-model="form.serviceFee" placeholder="年一次性服务费" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="年利率" prop="rate">
              <el-input v-model="form.rate" placeholder="请输入年利率"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-button type="primary" @click="getList">添加租房合同</el-button>
        <el-table :data="form.factoringDetailList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="房产信息" align="center" prop="roomName" />
          <el-table-column label="房源编号" align="center" prop="roomCode" />
          <el-table-column label="租客姓名" align="center" prop="tenantName" />
          <el-table-column label="租客手机号" align="center" prop="tenantPhone" />
          <el-table-column label="应收租约金额" align="center" prop="shouldMonthRent" />
          <el-table-column label="保理授信额度" align="center" prop="factoringCreditMoney" />
          <el-table-column label="保理利息" align="center" prop="factoringInterest" />
          <el-table-column label="借款期限" align="center">
            <template slot-scope="slot">
             <span>{{slot.row.factoringPeriod}}个月{{slot.row.borrowedStart}}--{{slot.row.borrowedEnd}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['financial:factoring:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-row>
          <el-col :span="10">
            <el-form-item label="应收租约合计" prop="rentMoney" >
              <el-input v-model="form.rentMoney" placeholder="应收租约合计" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="保理授信金额合计" prop="factoringCreditMoney" >
              <el-input v-model="form.factoringCreditMoney" placeholder="保理授信金额合计" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="服务费" prop="serviceMoney">
              <el-input v-model="form.serviceMoney" placeholder="服务费" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="保理利息合计" prop="factoringAdditionalMoney">
              <el-input v-model="form.factoringAdditionalMoney" placeholder="保理利息合计" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="实收保理金额" prop="factoringTrueMoney">
          <el-input v-model="form.factoringTrueMoney" placeholder="实收保理金额" disabled />
        </el-form-item>
        <el-form-item label="备注" prop="remark" >
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="submitForm(form)">确 定</el-button>

    <el-dialog
      title="添加租房合同"
      :visible.sync="dialogVisible"
      width="60%">
      <el-form :model="query">
        <el-form-item label="小区名称" label-width="120px">
          <el-input v-model="query.premiseName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="租客名称" label-width="120px">
          <el-input v-model="query.tenantName" autocomplete="off"></el-input>
        </el-form-item>
        <el-button type="primary" @click="getList">搜 索</el-button>
      </el-form>

      <el-table :data="contractList">
        <el-table-column property="tenantName" label="租客姓名" align="center"></el-table-column>
        <el-table-column property="roomName" label="房源信息" align="center"></el-table-column>
        <el-table-column property="signingDate" label="签约日志" value-format="yyyy-MM-dd HH:mm:ss" align="center"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="addContract(scope.row)"
              v-hasPermi="['financial:factoring:remove']"
            >选中</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listFactoring, delFactoring, addFactoring, getProduct, getContract, getSelectContract } from "@/api/financial/factoring";

export default {
  name: "Factoring",
  components: {
  },
  data() {
    return {
      // 对话框
      dialogVisible: false,
      //products
      products: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 租约保理申请表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        factoringDetailList:[
        ],
        contractId:null,
        businessId:null
      },
      // 表单校验
      rules: {
        jrProductId: [
          { required: true, message: "金融产品不能为空", trigger: "change" }
        ],
        serviceFee: [
          { required: true, message: "请输入年一次性服务费", trigger: "blur" }
        ],
        rentMoney: [
          { required: true, message: "请输入应收租约合计", trigger: "blur" }
        ],
        factoringCreditMoney: [
          { required: true, message: "请输入保理授信金额合计", trigger: "blur" }
        ],
        serviceMoney: [
          { required: true, message: "请输入服务费", trigger: "blur" }
        ],
        factoringAdditionalMoney: [
          { required: true, message: "请输入保理利息合计", trigger: "blur" }
        ],
        factoringTrueMoney: [
          { required: true, message: "请输入实收保理金额", trigger: "blur" }
        ],
        creditLine: [
          { required: true, message: "请输入授信额度", trigger: "blur" }
        ],
        rate: [
          { required: true, message: "请输入年利率", trigger: "blur" }
        ]
      },
      query:{
        premiseName:null,
        tenantName:null,
        contractId:null,
        rate: null,
        jrProductId: null
      }
    };
  },
  created() {
    this.getProduct();
  },
  methods: {
    /** 查询金融产品列表 */
    getProduct() {
      getProduct().then(response => {
        this.products = response.data;
      });
    },
    // 获取租房合同
    getList() {
      this.dialogVisible = true;
      getContract(this.query).then(response => {
        this.contractList = response.rows;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加租约保理申请";
    },
    /** 提交按钮 */
    submitForm(form) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addFactoring(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.$router.push('installment');
          })
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // 查找元素位置
      let index = this.form.factoringDetailList.findIndex(item => item == row);
      // 删除元素
      this.form.factoringDetailList.splice(index,1);
      // 计算
      let should = 0.0;
      let factoringCreditMoney = 0.0;
      let factoringAdditionalMoney = 0.0;
      let serviceMoney = 0.0;
      this.form.factoringDetailList.forEach(data => {
        should += data.shouldMonthRent;
        serviceMoney += data.serviceMoney;
        factoringCreditMoney += data.factoringCreditMoney;
        factoringAdditionalMoney += data.factoringInterest;
      })
      // 应收租约合计
      this.form.rentMoney = should;
      // 保理授信金额合计
      this.form.factoringCreditMoney = factoringCreditMoney;
      // 保理利息合计
      this.form.factoringAdditionalMoney = factoringAdditionalMoney;
      // 实收金额
      this.form.factoringTrueMoney =  should;
      // 服务费
      this.form.serviceMoney = serviceMoney;
    },
    selectCredLion(id) {
      this.products.forEach(product=>{
        if (product.id == this.form.jrProductId) {
          this.form.creditLine = product.creditLine;
        }
      })
    },
    addContract(row) {
      let should = 0.0;
      let factoringCreditMoney = 0.0;
      let factoringAdditionalMoney = 0.0;
      let serviceMoney = 0.0;
      this.query.contractId = row.id;
      this.query.rate = this.form.rate;
      this.query.jrProductId = this.form.jrProductId;
      getSelectContract(this.query).then(response => {
        response.data.forEach(data=>{
          this.form.businessId = data.businessId;
          console.log(data.businessId)
          this.form.factoringDetailList.push(data);
        })
        this.form.factoringDetailList.forEach(data=>{
          should += data.shouldMonthRent;
          serviceMoney += data.serviceMoney;
          factoringCreditMoney += data.factoringCreditMoney;
          factoringAdditionalMoney += data.factoringInterest;
          this.form.contractId = data.contractId;
        });
        // 应收租约合计
        this.form.rentMoney = should;
        // 保理授信金额合计
        this.form.factoringCreditMoney = factoringCreditMoney;
        // 保理利息合计
        this.form.factoringAdditionalMoney = factoringAdditionalMoney;
        // 实收金额
        this.form.factoringTrueMoney =  should;
        // 服务费
        this.form.serviceMoney = serviceMoney;
      });
      this.dialogVisible = false;
    }
  }
};
</script>
